﻿@model ProductItem
<div class="modal-dialog">
    <div class="modal-content">
        @using (Html.BeginForm())
        {
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;</button>
                <h4 class="modal-title">
                    修改库存</h4>
            </div>
            <div class="modal-body">
                <h4>@(Model.Name)</h4>
                @if (Model.ProductItemSkus.Count > 0)
                {
                    <div class="form-group">
                        <p class="form-control-static">
                            <strong>库存总量</strong> <span id="total">@(Model.Quantity)</span></p>
                    </div>
                    foreach (var o in Model.ProductItemSkus)
                    {
                    <div class="form-group">
                        <label for="quantity-@(o.Id)">
                            @foreach (var s in o.SkuProps)
                            {
                                <span>@(s.VName)</span>
                            }</label>
                        <input id="quantity-@(o.Id)" name="quantity" type="text" maxlength="8" value='@(o.Quantity)' class="form-control input-xlarge" onkeyup="checkValue(this);"/>
                        <input name="id" type="hidden" value='@(o.Id)' />
                    </div>
                    }
                }
                else
                {
                <div class="form-group">
                    <label for="inventory">
                        商品库存</label>
                    <input type="text" onkeyup="this.value=this.value.replace(/\D/g,'');" maxlength="8" class="form-control input-xlarge" value="@(Model.Quantity)" name="inventory" id="inventory" />
                </div>
                }
            </div>
            <div class="modal-footer">
                <p id="message" class="pull-left color-red">
                </p>
                <button type="button" class="btn btn-primary" onclick="submitForm(this.form,@(Model.Id))">
                    确 认</button>
            </div>
        }
    </div>
</div>
<script type="text/javascript">
    function checkValue(obj) {
        var total = 0;
        obj.value = obj.value.replace(/\D/g, '');
        
        var val = $(obj).val();
        if (!isNaN(val)) {
            var array = $('input[name=quantity]');
            $.each(array, function (i, o) {
                var quantity = $(o).val();
                total += parseInt(quantity);
            });
            $("#total").html(total);
        } else {
            $(obj).val('');
        }
    }
    function submitForm(form, itemId) {
        var param = $(form).serialize();
        $.postJSON(form.action, param, function (result) {
            if (result.Status) {
                $("#item-quantity-" + itemId).html(result.Data);
                $('#message', form).css('color', 'green').html("更新库存成功！");
            }
            else {
                $('#message', form).css('color', 'red').html(result.Message);
            }
        });
    }
</script>
